<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>批量导入弹窗</title>
    <style>
        table{border:1px solid; margin: 0px auto}
        td{border:1px solid; border-color: #000000; width: 400px; height: 50px}
        input{background-color: #4169e1;border:0;width: 400px; height: 50px; color: #ffffff
        }
    </style>

    <script type="text/javascript" src="../static/js/jquery-1.10.2.min.js"></script>

</head>

<body>
    <div>
        <table border="0" cellspacing="0" cellpadding="0">
            <tr bgcolor="#B0C4DE">
                <td ><input type="button" id="download-btn" value="下载问题列表模板excel" onclick="download()"></td>
                <!-- TODO js中doenload（）函数实现点击下载模板 -->
            </tr>

            <tr bgcolor="#B0C4DE">
                <td>
                    <input type="file" id="Excel-file">
                    <input type="button" id="upload-btn" value="填好后，上传问题列表文件" onclick="upload()">
                </td>
                <!-- TODO js中upload（）函数实现点击呼出文件上传页面 -->
            </tr>
        </table>
    </div>


    <script type="text/javascript" src="../static/js/jquery-1.10.2.min.js">
    </script>
    <script type="text/javascript">
        //下载模板页
        function download() {
            window.location.href = "http://localhost:8092/downloadExcel";
        }

        function upload() {
            //文件内容
            var file = $("#Excel-file").val();
            var fileflag = file.substring(file.lastIndexOf(".")+1).toLowerCase();
            if (file == "") {
                alert("上传失败，没有选择文件！");
                return;
            }else if (fileflag != "xls" && fileflag !="xlsx"){
                //判断文件是否为excel
                alert("上传失败，文件不是Excel文件！");
            } else {
                //ajax上传Excel
                var formData =new FormData();
                var username = '[[${user.username}]]';
                var sort = '[[${user.sort}]]';
                console.log(username)

                formData.append("file",$("#Excel-file")[0].files[0]);
                formData.append("username",username);
                formData.append("sort",sort);
                $.ajax({
                    type : "post",
                    url : "http://localhost:8092/uploadExcel",
                    data : formData,
                    processData : false,
                    contentType : false,
                    success: function (data) {
                        if (data.message=="上传成功"){
                            window.location.href = "http://localhost:8092/KnowledgeBase/close"
                        }else {
                            alert(data.message);
                        }
                    }
                })
            }
        }
    </script>
</body>
</html>